/**
 * 
 */
package com.novatialabs.qttt.gwt.client;

import java.util.List;
import java.util.Stack;

/**
 * @author nchalko
 * 
 */
public class GameModel {
  private final Stack<Move> moves = new Stack<Move>();

  public GameModel(List<Move> newGameMoves) {
    moves.clear();
    moves.addAll(newGameMoves);
  }

  public GameModel() {
  }

  public void mark(int cellNumber) {
    if (moves.isEmpty()) {
      pushNewMove(cellNumber);
      return;
    }
    Move top = moves.peek();
    switch (top.getState()) {
    case NOT_PLAYED:
      moves.pop();
      pushNewMove(cellNumber);
      break;
    case ENTANGLED:
      if (top.getSecondMark() == 0) {
        moves.pop();
        moves.push(new Move(top.getFirstMark(), cellNumber, Move.State.ENTANGLED));
      } else {
        pushNewMove(cellNumber);
      }
      break;
    case COLLAPSED_TO_FIRST:
      pushNewMove(cellNumber);
      break;
    case COLLAPSED_TO_SECOND:
      pushNewMove(cellNumber);
      break;
    default:
      break;
    }
  }

  private void pushNewMove(int cellNumber) {
    moves.push(new Move(cellNumber, 0, Move.State.ENTANGLED));
  }

  public List<Move> getMoves() {
    return moves;
  }

}
